package blog.com.mw.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.com.mw.base.ResultData;
import blog.com.mw.dao.ArticleDao;
import blog.com.mw.dao.DiscussDao;
import blog.com.mw.dao.UserDao;
import blog.com.mw.pojo.po.ArticlePO;
import blog.com.mw.pojo.po.DiscussPO;
import blog.com.mw.pojo.po.UserPO;
import blog.com.mw.pojo.vo.DiscussVO;
import blog.com.mw.service.DiscussService;

@Service
public class DiscussServiceImpl implements DiscussService {
	
	@Autowired
	private static Logger logger = LoggerFactory.getLogger(DiscussServiceImpl.class);

	@Autowired
	private UserDao userDao;
	@Autowired
	private DiscussDao dao;
	@Autowired
	private ArticleDao articleDao;
	@Override
	public ResultData getDiscussById(int id) {
		logger.info("退出getDiscussById方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			DiscussPO po = dao.getDiscussById(Long.valueOf(id));
			DiscussVO vo = new DiscussVO();
			UserPO user = userDao.getUserById(po.getUserId());
			vo.poToVo(po);
			vo.setDiscusser(user.getName());
			vo.setAvatar(user.getAvatar());
			if(po != null && !ObjectUtils.isEmpty(user)) {
				data = new HashMap<>();
				data.put("data", vo);
				rs = ResultData.getOk();
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("评论不存在！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出getDiscussById方法");
		return rs;
	}

	@Override
	public ResultData getDiscussByArticleId(int articleId) {
		logger.info("退出getDiscussByArticleId方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			List<DiscussPO> pos = dao.getDiscussByArticleId(Long.valueOf(articleId));
			List<DiscussVO> vos = new ArrayList<>();
			pos.forEach(item->{
				UserPO user = userDao.getUserById(item.getUserId());
				DiscussVO vo = new DiscussVO();
				vo.poToVo(item);
				vo.setDiscusser(user.getName());
				vo.setAvatar(user.getAvatar());
				vos.add(vo);
			});
			if(pos != null && !pos.isEmpty()) {
				data = new HashMap<>();
				data.put("data", vos);
				rs = ResultData.getOk();
				rs.setMsg(pos.size()>0?"获取成功！":"无数据！");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
				rs.setData(data);
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出getDiscussByArticleId方法");
		return rs;
	}

	@Override
	public ResultData getDiscussByUserId(int userId) {
		logger.info("退出getDiscussByUserId方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			List<DiscussPO> pos = dao.getDiscussByUserId(Long.valueOf(userId));
			if(pos != null) {
				data = new HashMap<>();
				List<DiscussVO> vos = new ArrayList<>();
				pos.forEach(item->{
					UserPO user = userDao.getUserById(item.getUserId());
					DiscussVO vo = new DiscussVO();
					vo.poToVo(item);
					vo.setDiscusser(user.getName());
					vo.setAvatar(user.getAvatar());
					vos.add(vo);
				});
				data.put("data", vos);
				rs = ResultData.getOk();
				rs.setMsg(pos.size()>0?"获取成功！":"无数据！");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
				rs.setData(data);
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出getDiscussByUserId方法");
		return rs;
	}
	
	@Transactional
	@Override
	public ResultData addDiscuss(DiscussVO vo) {
		logger.info("退出addDiscuss方法");
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			DiscussPO po = new DiscussPO();
			ArticlePO articlePo = articleDao.getArticleById(Long.valueOf(vo.getArticleId()));
			articlePo.setCommentCount(articlePo.getCommentCount()+1);
			articleDao.updateArticle(articlePo);
			vo.voToPo(po);
			if(dao.addDiscuss(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("添加成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("添加失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出addDiscuss方法");
		return rs;
	}

	@Override
	public ResultData delDiscuss(List<Integer> ids) {
		logger.info("退出delDiscuss方法");
		ResultData rs = null;
		try {
			if(ids.size()==0) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			int delSize = dao.delDiscuss(ids);
			if(delSize>0) {
				rs = ResultData.getOk();
				rs.setMsg(delSize==ids.size()?"删除成功！":"部分数据未删除成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("删除失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出delDiscuss方法");
		return rs;
	}

	@Override
	public ResultData getAllDiscuss(int pageNum,int pageSize,String keyword) {
		logger.info("进入getAllDiscuss方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<DiscussPO> discussPos = dao.getAllDiscuss(keyword);
			if(discussPos != null && !discussPos.isEmpty()) 
			{	
				List<DiscussVO> discussVos = new ArrayList<>();
				discussPos.forEach(item->{
					UserPO user = userDao.getUserById(item.getUserId());
					DiscussVO vo = new DiscussVO();
					vo.poToVo(item);
					vo.setAvatar(user.getAvatar());
					vo.setDiscusser(user.getName());
					discussVos.add(vo);
				});
				PageInfo<DiscussVO> pageInfo = new PageInfo<DiscussVO>(discussVos);
				pageInfo.setTotal(dao.getAllDiscuss(keyword).size());
				data = new HashMap<>();
				data.put("data", pageInfo);
				rs = ResultData.getOk();
				rs.setMsg("获取成功");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出getAllDiscuss方法");
		return  rs;
	}

	@Override
	public ResultData updateDiscuss(DiscussVO vo) {
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			DiscussPO po = new DiscussPO();
			po.setId(Long.valueOf(vo.getId()));
			po.setContent(vo.getContent());
			if(dao.updateDiscuss(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("修改成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("修改失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

}
